import pandas as pd
import numpy as np
import itertools

xlsx_name = 'D:/repos/sicost/属性.xlsx'
df = pd.read_excel(xlsx_name)
var_list = df.columns.tolist()
var_list = var_list[1:]
df.fillna(1, inplace=True)
def cal_conter_var(var1,var2):
    df_tmp = df.copy()
    if var2 != '':
        df_tmp['new_var'] = df[var1] * df[var2]
    else:
        df_tmp['new_var'] = df[var1]
    counter_df_tmp = df_tmp[df_tmp['new_var'] > 1]
    counter_df_tmp = counter_df_tmp.reset_index(drop=True)
    conter_var_list = counter_df_tmp['攻击方'].tolist()
    return conter_var_list
conter_var_list = cal_conter_var('龙','地面')
xlsx_name = 'D:/repos/sicost/阿尔宙斯.xlsx'
df1 = pd.read_excel(xlsx_name)
df1.VAR1.fillna('', inplace=True)
df1.VAR2.fillna('', inplace=True)

df1_1 = df1[df1['TYPE'] == 1]
df1_1 = df1_1.reset_index(drop=True)
df1_2 = df1[df1['EVOLUTION'] == 1]
df1_2 = df1_2.reset_index(drop=True)
df_out = pd.DataFrame(columns=var_list)
for index, row in df1.iterrows():
    new_counter_list = cal_conter_var(row['VAR1'], row['VAR2'])
    dict = {}
    for var_tmp in new_counter_list:
        dict[var_tmp] = 1
    new_row = pd.Series(dict)
    df_out = df_out.append(new_row, ignore_index=True)


df_out.fillna(0, inplace=True)
df_out_sum = pd.DataFrame(columns=var_list)
dict = {}
for var_tmp in var_list:
    sum_tmp = df_out[var_tmp].sum()
    dict[var_tmp] = sum_tmp
new_row = pd.Series(dict)
df_out_sum = df_out_sum.append(new_row, ignore_index=True)


df_out2 = pd.DataFrame(columns=['VAR1', 'VAR2', 'VAR3', 'VAR4', 'PM_NUM'])
combinations = itertools.combinations(var_list, 4)
for combo in combinations:
    combo_list = list(combo)
    print(combo_list)
    var1 = combo_list[0]
    var2 = combo_list[1]
    var3 = combo_list[2]
    var4 = combo_list[3]
    df2 = df_out.copy()
    pm_num = 0
    for index, row in df2.iterrows():
        if row[var1] == 1 or row[var2] == 1 or row[var3] == 1 or row[var4] == 1:
            pm_num = pm_num + 1
    dict = {}
    dict['VAR1'] = var1
    dict['VAR2'] = var2
    dict['VAR3'] = var3
    dict['VAR4'] = var4
    dict['PM_NUM'] = pm_num
    new_row = pd.Series(dict)
    df_out2 = df_out2.append(new_row, ignore_index=True)
# var1 = '地面'
# var2 = ''
# if var2 != '':
#     print('双属性')
#     my_list = [i for i in var_list if i != var1]
#     my_list = [i for i in my_list if i != var2]
#     combinations = itertools.combinations(my_list, 2)
#     for combo in combinations:
#         combo_list = list(combo)
#         print(combo_list)
#         var3 = combo_list[0]
#         var4 = combo_list[1]
#         df2 = df_out.copy()
#         pm_num = 0
#         for index, row in df2.iterrows():
#             if row[var1] == 1 or row[var2] == 1 or row[var3] == 1 or row[var4] == 1:
#                 pm_num = pm_num + 1
#         dict = {}
#         dict['VAR1'] = var1
#         dict['VAR2'] = var2
#         dict['VAR3'] = var3
#         dict['VAR4'] = var4
#         dict['PM_NUM'] = pm_num
#         new_row = pd.Series(dict)
#         df_out2 = df_out2.append(new_row, ignore_index=True)
#
# else:
#     print('单属性')
#     my_list = [i for i in var_list if i != var1]
#     combinations = itertools.combinations(my_list, 3)
#     for combo in combinations:
#         combo_list = list(combo)
#         print(combo_list)
#         var3 = combo_list[0]
#         var4 = combo_list[1]
#         var5 = combo_list[2]
#         df2 = df_out.copy()
#         pm_num = 0
#         for index, row in df2.iterrows():
#             if row[var1] == 1 or row[var3] == 1 or row[var4] == 1 or row[var5] == 1:
#                 pm_num = pm_num + 1
#         dict = {}
#         dict['VAR1'] = var1
#         dict['VAR2'] = var3
#         dict['VAR3'] = var4
#         dict['VAR4'] = var5
#         dict['PM_NUM'] = pm_num
#         new_row = pd.Series(dict)
#         df_out2 = df_out2.append(new_row, ignore_index=True)
sorted_df_out2 = df_out2.sort_values(by='PM_NUM', ascending=False)

writer = pd.ExcelWriter('result0912_20.xlsx')
sorted_df_out2.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()
print('finish')








